﻿@using System.Text

@functions {
    public static string GetAttrString(string name, object value)
    {
        return string.Format(" {0}=\"{1}\"", name, value);
    }

    public static void AddAttributes(Dictionary<string, object> dic, object option)
    {
        if (option == null)
            return;

        var props = option.GetType().GetProperties();
        foreach (var prop in props)
        {
            dic[prop.Name] = prop.GetValue(option);
        }
    }
}

@helper RenderControl(string type, string id, bool required = false, object option = null, object defaultOption = null)
{
    var dic = new Dictionary<string, object>();
    if (required)
    {
        dic["required"] = "true";
    }
    AddAttributes(dic, defaultOption);
    AddAttributes(dic, option);

    var sb = new StringBuilder();
    foreach (var key in dic.Keys)
    {
        sb.AppendFormat(" {0}=\"{1}\"", key, dic[key]);
    }
    var attrs = sb.ToString();

    <input class="@type" id="@id" name="@id" @(new HtmlString(attrs)) />
}

@helper Hidden(params string[] ids)
{
    foreach (var id in ids)
    {
        @RenderControl("mini-hidden", id)
    }
}

@helper TextBox(string id, bool required = false, object option = null)
{
    @RenderControl("mini-textbox", id, required, option)
}

@helper Password(string id, bool required = false, object option = null)
{
    @RenderControl("mini-password", id, required, option)
}

@helper TextArea(string id, bool required = false, object option = null)
{
    @RenderControl("mini-textarea", id, required, option)
}

@helper DatePicker(string id, bool required = false, object option = null)
{
    @RenderControl("mini-datepicker", id, required, option)
}

@helper Spinner(string id, bool required = false, object option = null)
{
    @RenderControl("mini-spinner", id, required, option)
}

@helper TimeSpinner(string id, bool required = false, object option = null)
{
    @RenderControl("mini-timespinner", id, required, option)
}

@helper CheckBox(string id, bool required = false, object option = null)
{
    var defOption = new { trueValue = "Y", falseValue = "N" };
    @RenderControl("mini-checkbox", id, required, option, defOption)
}

@helper ComboBox(string id, bool required = false, object option = null)
{
    var defOption = new { showNullItem = "true" };
    @RenderControl("mini-combobox", id, required, option, defOption)
}

@helper TreeSelect(string id, bool required = false, object option = null)
{
    var defOption = new { textField = "text", valueField = "id" };
    @RenderControl("mini-treeselect", id, required, option, defOption)
}

@helper CheckBoxList(string id, bool required = false, object option = null)
{
    var defOption = new { textField = "text", valueField = "id" };
    @RenderControl("mini-checkboxlist", id, required, option, defOption)
}

@helper RadioButtonList(string id, bool required = false, object option = null)
{
    var defOption = new { textField = "text", valueField = "id" };
    @RenderControl("mini-radiobuttonlist", id, required, option, defOption)
}

@helper ListBox(string id, bool required = false, object option = null)
{
    var defOption = new { textField = "text", valueField = "id" };
    @RenderControl("mini-listbox", id, required, option, defOption)
}